<?php
/***********************************************************************
 * @package    	IvanEngine
 * @subpackage 	Core
 * @author     	Ivan <ivanzx@msn.com>
 * @created    	2010-05-13
 ***********************************************************************/
if(defined("IVANENGINE_CORE_EXCEPTION_VALIDATOR"))
	die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_EXCEPTION_VALIDATOR", true);

require_once (dirname(__FILE__).'/Exception.php');
require_once (dirname(__FILE__).'/../Validator/ValidatorBase.php');

class CExceptionValidator extends CException 
{
	protected
	    $m_validator = null,
	    $m_arguments = array();
	    
	public function __construct(CValidatorBase $validator, $code, $arguments = array())
	{
		$this->m_validator = $validator;
    	$this->m_arguments = $arguments;

    	// override default exception message and code
    	$this->code = $code;

    	$tempMessage = $this->m_validator->GetMessage($this->code);
	    if (!$tempMessage)
	    {
	      	$tempMessage = $this->getMessage();
	    }
	    
	    $arguments = array();
	    foreach ($this->m_arguments as $key => $value)
	    {
	      if (is_array($value))
	      {
	        continue;
	      }
	      $arguments["%$key%"] = htmlspecialchars($value, ENT_QUOTES, CValidatorBase::GetCharset());
	    }
		$tempMessage = strtr($tempMessage, $arguments);

    	parent::__construct ($tempMessage);
	}
}
?>